#include "sdrv_file.h"

void file_rever(char *buf)
{
    char number[128] = {0};    //
    char file_name[128] = {0}; // 分析字符串
    char file_size[128] = {0};
    char file_dir[1024] = {0}; // 文件路径
    FILE_str(buf, number, file_name, file_size);
    sprintf(file_dir, "./%s/%s", number, file_name); // 拼接文件路径
    // b、打开文件(创建、可写)
    FILE *fp = fopen(file_dir, "w+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return;
    }
    struct device *p = head;
    while (p != NULL)
    {
        if (strstr(p->device_name, number)) // 找到设备
        {
            while (1)
            {
                // 读取来自客户端的数据
                char file_buf[1024] = {0};
                int ret = read(p->socket, file_buf, sizeof(file_buf));
                if (ret <= 0)
                {
                    break;
                }
                if (strstr(file_buf, "end"))
                {
                    break;
                }
                // 将读取到的数据，写入到本地的文件中
                fwrite(file_buf, ret, 1, fp);
            }
        }
        p = p->next;
    }
}

int FILE_str(char *buf, char *number, char *file_name, char *file_size)
{
    char *p = strtok(buf, ",");
    int i = 0;
    while (p != NULL)
    {
        printf("p1 = %s\n", p);
        if (i == 0)
            ;
        else if (i == 1)
            strcpy(number, p);
        else if (i == 2)
            strcpy(file_name, p);
        else if (i == 3)
            strcpy(file_size, p);
        i++;
        p = strtok(NULL, ",");
    }
    return 0;
}

void *FILE_send(void *buf)
{
    char number[128] = {0};    //
    char file_name[128] = {0}; // 分析字符串
    char file_size[128] = {0};
    char file_dir[1024] = {0}; // 文件路径
    FILE_str(buf, number, file_name, file_size);
    FILE *fp = fopen("./Data/forever_commodity", "r");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return NULL;
    }
    struct device *p = head;
    while (p != NULL)
    {
        if (strstr(p->device_name, number)) // 找到设备
        {
            write(p->socket, "file,1001,forever_commodity,0", strlen("file,1001,forever_commodity,0"));
            sleep(2);
            while (1)
            {
                // 读取来自客户端的数据
                char file_buf[1024] = {0};
                int ret = fread(file_buf, 1, sizeof(file_buf), fp);
                if (ret <= 0)
                {
                    break;
                }
                // 将读取到的数据，写入到本地的文件中
                write(p->socket, file_buf, ret);
            }
        }
    }
}

void *keep(void *arg)
{
    while (1)
    {
        pthread_mutex_lock(&keep_lock);
        struct device *p = head->next;
        while (p != NULL)
        {
            if (strstr(p->device_name, "user"))
            {
                continue;
            }
            write(p->socket, "keep", strlen("keep"));
            p = p->next;
        }
        pthread_mutex_unlock(&keep_lock);
        sleep(2);
    }
}

int tcp_file(char *file_name, char *tcp, struct device *p)
{
    char file_dir[1024] = {0};
    sprintf(file_dir, "/root/kkcode/second_item/Data/commodity_picture/%s", file_name);
    FILE *fp = fopen(file_dir, "a+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return -1;
    }
    while (1)
    {
        char buf[1024] = {0};
        int size = read(p->socket, buf, sizeof(buf));
        if (size <= 0)
        {
            printf("%s 设备断开连接\n", p->device_name);
            return -1;
        }
        if (strstr(buf, "end"))
        {
            break;
        }
        my_sand(buf, tcp);
    }
    my_sand("end", tcp);
    fclose(fp);
}

int go_on(char *buf)
{
    char name[255];  // 商品名称
    char id[255];    // 商品编号
    char price[255]; // 商品价格
    char type[255];  // 商品类型
    printf("p = %s\n", buf);
    char *p = strtok(buf, ",");
    int i = 0;
    while (p != NULL)
    {
        printf("p = %s\n", p);
        if (i == 1)
            strcpy(name, p);
        else if (i == 2)
            strcpy(id, p);
        else if (i == 3)
            strcpy(price, p);
        else if (i == 4)
            strcpy(type, p);
        i++;
        p = strtok(NULL, ",");
    }
    printf("name = %s\n", name);
    commodity_list_t *new_commodity = (commodity_list_t *)malloc(sizeof(commodity_list_t));
    if (new_commodity == NULL)
    {
        printf("内存分配失败\n");
        return -1;
    }
    new_commodity->commodity = (commodity_t *)malloc(sizeof(commodity_t));
    if (new_commodity->commodity == NULL)
    {
        printf("内存分配失败\n");
        free(new_commodity); // 释放之前分配的内存
        return -1;
    }
    // 将解析出来的数据填充到新节点中
    printf("=======1======\n");
    strcpy(new_commodity->commodity->commodity_name, name);
    printf("=======2======\n");
    strcpy(new_commodity->commodity->commodity_id, id);
    printf("=======3======\n");
    strcpy(new_commodity->commodity->commodity_price, price);
    printf("=======4======\n");
    strcpy(new_commodity->commodity->commodity_type, type);
    printf("=======5======\n");
    new_commodity->commodity_num = 10;
    printf("name = %s\n", new_commodity->commodity->commodity_name);
    commodity_list_t *q = commodity_head;
    while (q->next != NULL)
    {
        q = q->next;
    }
    q->next = new_commodity;
}

int write_go_on(char *buf)
{
    FILE *fp = fopen("/root/kkcode/second_item/Data/forever_commodity", "a+");
    if (fp == NULL)
    {
        printf("文件打开失败\n");
        return -1;
    }
    char *p = strtok(buf, ",");
    char *buf1 = buf + strlen(p) + 1;
    buf1[strlen(buf1) - 1] = '\0';
    fprintf(fp, "\n%s", buf1);
    fclose(fp);
}

